﻿#region Using References

using System;
using System.Diagnostics.Contracts;
using Beaker.OpenCube.NbtEditor.ViewModels;
using GalaSoft.MvvmLight.Messaging;

#endregion

namespace Beaker.OpenCube.NbtEditor.Messages
{
	/// <summary>
	/// Message for opening a view for a given view model
	/// </summary>
	[Pure]
	sealed class ShowViewMessage
		: MessageBase
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="ShowViewMessage" /> class for sending a message to open a view.
		/// </summary>
		/// <param name="viewModel">The view model.</param>
		/// <param name="onResultAvailable">The action to call when a result is available.</param>
		public ShowViewMessage( ViewModel viewModel, Action<ViewModel> onResultAvailable = null )
		{
			Contract.Requires( viewModel != null );
			Contract.Ensures( _viewModel == viewModel );
			Contract.Ensures( _onResultAvailable == onResultAvailable );

			_viewModel = viewModel;
			_onResultAvailable = onResultAvailable;
		}

		[ContractInvariantMethod]
		private void ContractInvariant()
		{
			Contract.Invariant( _viewModel != null );
		}


		private readonly ViewModel _viewModel;

		/// <summary>
		/// Gets the view model the dialog should be opened for.
		/// </summary>
		/// <value>
		/// The view model  the dialog should be opened for.
		/// </value>
		public ViewModel ViewModel
		{
			get
			{
				Contract.Ensures( Contract.Result<ViewModel>() != null );
				return _viewModel;
			}
		}

		private readonly Action<ViewModel> _onResultAvailable;

		/// <summary>
		/// Sends the signal that a result is available.
		/// </summary>
		public void SendResultAvailable()
		{
			if( _onResultAvailable != null )
				_onResultAvailable( ViewModel );
		}
	}
}
